rm(list=ls())

library(survey)
library(foreign)
library(doBy)
library(xtable)
library(ggplot2)
cces <- read.dta("CCES_2014b.dta")

cces$age_exact<-2014-cces$age_exact

## Create variable with self-report of which party they voted for in the primary
## 1=democrat, 0=republican
cces$presprimary_party<-NA
cces$presprimary_party[cces$cc325a==1 |cces$cc325a==2 | cces$cc325a==3 |cces$cc325a==4] <-1
cces$presprimary_party[cces$cc325b==1 |cces$cc325b==2 | cces$cc325b==3 |cces$cc325b==4] <-1
cces$presprimary_party[cces$cc325a==5 |cces$cc325a==6 | cces$cc325a==7 |cces$cc325a==8 |cces$cc325a==9 |cces$cc325b==14] <-0
cces$presprimary_party[cces$cc325a==5 |cces$cc325b==6 | cces$cc325b==7 |cces$cc325b==8 |cces$cc325b==9 |cces$cc325b==14] <-0

cces$presgeneral_party<-NA
cces$presgeneral_party[cces$congress_post_vote==1] <-"Democrat"
cces$presgeneral_party[cces$congress_post_vote==0] <-"Republican"


primary_type_dems <- read.csv("Primary Dates and Types 2008-2014 - Dems.csv",colClasses="character")
primary_type_reps <- read.csv("Primary Dates and Types 2008-2014 - Reps.csv",colClasses="character")


# reduce data to just primaries
#data <- data[data$Republican_type=="primary",]
primary_type<-as.data.frame(cbind(primary_type_dems$abb,  primary_type_dems$X2008.President.Type, primary_type_reps$X2008.President.Type))

# merge the data
cces14 <- cbind(primary_type[match(cces$abb,primary_type$abb),],
              cces)


# normalize ideology estimates
cces14$est <- (cces14$est - mean(cces14$est,na.rm=T))/sd(cces14$est)

# scap4 = retrospective primary choice (september)
# bcap4 = prospective primary choice (baseline)
# different primary dates...
# p = post, j = january, m = march, o=october
# e2012p = validated primaryturnout & registration
cces14$primaryturnout <- NA
# first, use known primary primaryturnout and self reports of *which* primary
cces14$primaryturnout[cces14$congress_post_vote=="0" & cces14$voted_primary_validated == "1" ] <- "Republican"
cces14$primaryturnout[cces14$congress_post_vote=="1" & cces14$voted_primary_validated == "1" ] <- "Democrat"

## Tidy up the data first
library(survey)

cces14$pid7<-as.numeric(as.vector(cces14$pid7))

cces14$voted_primary_validated[is.na(cces14$voted_primary_validated) & !is.na(cces14$congress_post_vote)]<-0

cces14$general_noprimary[cces14$voted_general_validated==1 & cces14$voted_primary_validated==0]<-1
cces14$general_noprimary[is.na(cces14$general_noprimary) & !is.na(cces14$congress_post_vote)]<-0

cces14$gender<-as.numeric(as.vector(cces14$gender))
cces14$idealpoint<-as.numeric(as.vector(cces14$est))
cces14$self_ideology<-as.numeric(as.vector(cces14$self_ideology))
cces14$donor<-as.numeric(as.vector(cces14$donor))
cces14$political_interest2<- as.numeric(as.vector(cces14$political_interest2))
cces14$hs[cces14$education==2]<-1
cces14$hs[cces14$education!=2]<-0

cces14$pol_interest[cces14$political_interest2==1]<-1
cces14$pol_interest[cces14$political_interest2!=1]<-0

cces14$college[cces14$education>=4]<-1
cces14$college[cces14$education<4]<-0

cces14$white[cces14$race==1]<-1
cces14$white[cces14$race!=1]<-0

cces14<-subset(cces14, !is.na(weight) )



cces14$weight<-as.numeric(as.vector(cces14$weight))
cces14.design<-svydesign(ids=cces14$respondent, data = cces14, weights=cces14$weight)

cces14_primary<-subset(cces14,primaryturnout!="0")
cces14_primary.design<-svydesign(ids=cces14_primary$respondent, data = cces14_primary, weights=cces14_primary$weight)

cces14_general<-subset(cces14,cces14$voted_general_validated==1 )
cces14_general.design<-svydesign(ids=cces14_general$respondent, data = cces14_general, weights=cces14_general$weight)


cces14_general_noprim<-subset(cces14,(voted_general_validated==1 & voted_primary_validated!=1))
cces14_general_noprim.design<-svydesign(ids=cces14_general_noprim$respondent, data = cces14_general_noprim, weights=cces14_general_noprim$weight)


## cross-tabs of who voted
# by party

cces14$voted_primary_validated<-as.numeric(as.vector(cces14$voted_primary_validated))
cces14$general_noprimary<-as.numeric(as.vector(cces14$general_noprimary))
cces14$voted_primary_validated<-as.numeric(as.vector(cces14$voted_primary_validated))
cces14$general_noprimary<-as.numeric(as.vector(cces14$general_noprimary))


svymean(cces14$voted_primary_validated[cces14$pid7<4], design=cces14.design[cces14$pid7<4], na.rm=T)
svymean(cces14$general_noprimary[cces14$pid7<4], design=cces14.design[cces14$pid7<4], na.rm=T)

svymean(cces14$voted_primary_validated[cces14$pid7>4], design=cces14.design[cces14$pid7>4], na.rm=T)
svymean(cces14$general_noprimary[cces14$pid7>4], design=cces14.design[cces14$pid7>4], na.rm=T)

## make density graph of ideal points
mean_idealpoint_primary_dems <- svymean(cces14_primary$idealpoint[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)

mean_idealpoint_partyfollowing_dems <- svymean(cces14_general$idealpoint[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

mean_idealpoint_primary_reps <- svymean(cces14_primary$idealpoint[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)

mean_idealpoint_partyfollowing_reps <- svymean(cces14_general$idealpoint[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)


cces14$Party<-cces14$pid3
cces14$Party[cces14$pid3==2]<-NA

fulltable <- NULL

## age
col1<-svyquantile(~cces14_primary$age_exact[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T, quantile=.5)
col2<-svyquantile(~cces14_general_noprim$age_exact[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T, quantile=.5)
col3<-svyquantile(~cces14_general$age_exact[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T, quantile=.5)

col4<-svyquantile(~cces14_primary$age_exact[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T, quantile=.5)
col5<-svyquantile(~cces14_general_noprim$age_exact[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T, quantile=.5)
col6<-svyquantile(~cces14_general$age_exact[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T, quantile=.5)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "age"; fulltable <- rbind(fulltable,mat)

## hs degree
col1<-svymean(cces14_primary$hs[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$hs[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$hs[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$hs[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$hs[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$hs[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "hs degree"; fulltable <- rbind(fulltable,mat)

## college degree
col1<-svymean(cces14_primary$college[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$college[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$college[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$college[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$college[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$college[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "college"; fulltable <- rbind(fulltable,mat)

## white
col1<-svymean(cces14_primary$white[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$white[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$white[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$white[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$white[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$white[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "white"; fulltable <- rbind(fulltable,mat)


## very interested in politics
col1<-svymean(cces14_primary$pol_interest[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$pol_interest[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$pol_interest[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$pol_interest[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$pol_interest[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$pol_interest[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "very interested"; fulltable <- rbind(fulltable,mat)


## ideological self-placement
col1<-svymean(cces14_primary$self_ideology[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$self_ideology[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$self_ideology[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$self_ideology[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$self_ideology[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$self_ideology[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "self-placement"; fulltable <- rbind(fulltable,mat)




## Issue ideology
##CCES
col1<-svymean(cces14_primary$idealpoint[cces14_primary$primaryturnout=="Democrat" & cces14_primary$source=="CCES_2014"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat" & cces14_primary$source=="CCES_2014"], na.rm=T)
col2<-svymean(cces14_general_noprim$idealpoint[cces14_general_noprim$presgeneral_party=="Democrat" & cces14_general_noprim$source=="CCES_2014"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat" & cces14_general_noprim$source=="CCES_2014"], na.rm=T)
col3<-svymean(cces14_general$idealpoint[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4 & cces14_general$source=="CCES_2014"], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4 & cces14_general$source=="CCES_2014"], na.rm=T)

col4<-svymean(cces14_primary$idealpoint[cces14_primary$primaryturnout=="Republican" & cces14_primary$source=="CCES_2014"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican" & cces14_primary$source=="CCES_2014"], na.rm=T)
col5<-svymean(cces14_general_noprim$idealpoint[cces14_general_noprim$presgeneral_party=="Republican" & cces14_general_noprim$source=="CCES_2014"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican" & cces14_general_noprim$source=="CCES_2014"], na.rm=T)
col6<-svymean(cces14_general$idealpoint[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4 & cces14_general$source=="CCES_2014"], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4 & cces14_general$source=="CCES_2014"], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "ideology"; fulltable <- rbind(fulltable,mat)

##########
## Issues
##########

# Abortion
col1<-svymean(cces14_primary$cces2014_abortionalwaysallow[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$cces2014_abortionalwaysallow[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$cces2014_abortionalwaysallow[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$cces2014_abortionalwaysallow[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$cces2014_abortionalwaysallow[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$cces2014_abortionalwaysallow[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(100-col1*100,0), "%",sep="")
col2<-paste(round(100-col2*100,0), "%",sep="")
col3<-paste(round(100-col3*100,0), "%",sep="")
col4<-paste(round(100-col4*100,0), "%",sep="")
col5<-paste(round(100-col5*100,0), "%",sep="")
col6<-paste(round(100-col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "abortion"; fulltable <- rbind(fulltable,mat)

# Health Reform 
col1<-svymean(cces14_primary$cces2012_supportaca[cces14_primary$primaryturnout=="Democrat"], design=cces14_primary.design[cces14_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces14_general_noprim$cces2012_supportaca[cces14_general_noprim$presgeneral_party=="Democrat"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces14_general$cces2012_supportaca[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], design=cces14_general.design[cces14_general$presgeneral_party=="Democrat" | cces14_general$pid7<4], na.rm=T)

col4<-svymean(cces14_primary$cces2012_supportaca[cces14_primary$primaryturnout=="Republican"], design=cces14_primary.design[cces14_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces14_general_noprim$cces2012_supportaca[cces14_general_noprim$presgeneral_party=="Republican"], design=cces14_general_noprim.design[cces14_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces14_general$cces2012_supportaca[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], design=cces14_general.design[cces14_general$presgeneral_party=="Republican" | cces14_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "health reform"; fulltable <- rbind(fulltable,mat)


fulltable
xtab <- print(xtable(fulltable))
cat(xtab,file="cces2014.tex")

